package com.bona.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bona.dao.common.BaseDao;
import com.bona.dao.system.SysFunctionDao;
import com.bona.model.system.SysFunctionModel;
import com.bona.service.common.BaseService;
import com.bona.util.common.StringUtil;
import com.bona.util.common.UUIDGenerator;

/**
 * 
 * <br>
 * <b>功能：</b>SysFunctionService<br>
 * <b>作者：</b>tbzeng<br>
 * <b>日期：</b> Dec 9, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，tbzeng<br>
 */
@Service("functionService")
@Transactional
public class SysFunctionService extends BaseService {
	
	private final static Logger logger = Logger.getLogger(SysFunctionService.class);
	
	@Autowired
	private SysFunctionDao functionDao;
	
	/**
	 * 获取所有id的菜单
	 * @param ids
	 * @return
	 */
	public List<SysFunctionModel> queryFunctionByIds(List<String> ids) {
	
		List<SysFunctionModel> models = functionDao.queryFunctionByIds(ids);
		return models;
	}
	
	/**
	 * 获取角色下的所有菜单
	 * @param roleIds
	 * @return
	 */
	public List<SysFunctionModel> queryFunctionByRoleIds(List<String> roleIds) {
		
		List<SysFunctionModel> models = functionDao.queryFunctionByRoleIds(roleIds);
		return models;
	}
	
	/**
	 * 添加或修改菜单
	 * @param model
	 */
	public void addOrUpdate(SysFunctionModel model) {
	
		if (model == null) return;
		if (StringUtil.isEmpty(model.getFunctionorder())) {
			model.setFunctionorder("0");
		}
		String functionpid = model.getParentfunctionid();
		if (StringUtil.isNotEmpty(functionpid)) {
			SysFunctionModel parentFunction = query(new SysFunctionModel(functionpid));
			model.setFunctionlevel(parentFunction.getFunctionlevel()+1);
		} 
		if (StringUtil.isEmpty(model.getId())) {	// 添加菜单
			model.setId(UUIDGenerator.generate());
			functionDao.add(model);
			return;
		}
		
		String functionname = model.getFunctionname();
		Integer functiontype = model.getFunctiontype();
		Integer functionlevel = model.getFunctionlevel();
		String functionurl = model.getFunctionurl();
		String iconid = model.getIconid();
		String functionorder = model.getFunctionorder();
		String deskIconid = model.getDeskIconid();
		String parentfunctionid = model.getParentfunctionid();
		model = query(new SysFunctionModel(model.getId()));
		model.setFunctionname(functionname);
		model.setFunctiontype(functiontype);
		model.setFunctionlevel(functionlevel);
		model.setFunctionurl(functionurl);
		model.setIconid(iconid);
		model.setDeskIconid(deskIconid);
		model.setFunctionorder(functionorder);
		model.setParentfunctionid(parentfunctionid);
		
		// 更新下级菜单项
		List<String> idList = functionDao.queryChildFunctionIdsByPid(model.getId());
		if (idList!=null && idList.size()>0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("idlist", idList);
			map.put("pid", model.getId());
			functionDao.updatePidToCurrentId(map);
		}
		// 更新角色菜单关联关系
		List<String> rfIdList = functionDao.queryRoleFunctionByFuncId(model.getId());
		if (rfIdList!=null && rfIdList.size()>0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("idlist", rfIdList);
			map.put("funcid", model.getId());
			functionDao.updateFuncIdToCurrentId(map);
		}
		// 更新菜单
		functionDao.update(model);
		logger.info("菜单添加修改["+model+"]");
	}
	
	/**
	 * 删除菜单
	 * @param funcId
	 */
	public void deleteFunction(String funcId) {
		
		// 1、获取当前菜单的所有子菜单
		List<String> childIdLis = new ArrayList<String>();
		queryChildList(childIdLis, funcId);
		childIdLis.add(funcId);	// 加入当前菜单id
		// 2、删除所有菜单包含子菜单， 菜单角色关联关系
		int rfCnt = functionDao.deleteRoleFunctionByFuncId(childIdLis);
		// 3、删除菜单
		int idCnt = functionDao.delete(childIdLis);
		logger.info("删除菜单，菜单角色关联关系["+rfCnt+"],菜单项["+idCnt+"]");
	}
	
	/**
	 * 获取所有子菜单
	 * @param idList
	 * @param parentId
	 * @param level
	 */
	public void queryChildList(List<String> idList, String parentId){   

		List<String> list = new ArrayList<String>();   
		try{   
			list = functionDao.queryChildFunctionIdsByPid(parentId);	//每次查询出上级为的分类 
			if(list != null && list.size() > 0){   
				for(int i=0;i<list.size();i++){   
					idList.add(list.get(i));  
					queryChildList(idList, list.get(i));   //递归查询
				}   
			}
		}catch(Exception e){   
			e.printStackTrace();   
		}   
	}
	
	@Override
	public BaseDao getDao() {
		
		return functionDao;
	}

}
